﻿/*!	@file
	@brief	アロケータ(ブロックタイプ)
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


#include "r_alloc_block.h"
#include "r_alloc_system.h"
#include <string.h>
#include "../util/r_util_round.h"
#include "../r_debug.h"


RALLOC_PLACEMENT_NEW_DELETE_IMPLEMENT_SYSTEM ( cRAllocBlock )

cRAllocBlock :: cRAllocBlock ( const r_cstr name, r_si32 size, r_si32 num, r_si32 align )
	:
	blockSize ( size ),
	blockNum ( num ),
	flagNum ( r_tm_roundup<32> ( num ) ),
	flagTop ( NULL ),
	blockTop ( NULL ),
	addrTop ( NULL ),
	cRAllocBase ( name, size * num, align )
{
	RDEBUG_REPORT ( _RST ( "cRAllocBlock [%d] [%d]%08x\n" ), size, num, this );
	cRAllocSystem::instance systemalloc;
	r_si32 tmpsize = blockSize * blockNum + flagNum * sizeof ( r_si32 );
	addrTop = systemalloc->malloc ( tmpsize );
	flagTop = reinterpret_cast<r_ui32*> ( addrTop );
	blockTop = reinterpret_cast<void*> ( reinterpret_cast<size_t> ( addrTop ) + ( flagNum * sizeof ( r_si32 ) ) );
	memset ( flagTop, 0, flagNum * sizeof ( r_si32 ) );
}

cRAllocBlock :: ~cRAllocBlock ( void )
{
	cRAllocSystem::instance systemalloc;
	systemalloc->free ( addrTop );
	RDEBUG_REPORT ( _RST ( "remove cRAllocStack %08x\n" ), this );
}


void* cRAllocBlock :: malloc ( size_t size )
{
	if ( size > blockSize ) {
		RDEBUG_REPORT ( _RST ( "size[%d] is over block size[%d]\n" ), size, blockSize );
		return NULL;
	}
	// 空きブロックを探す
	for ( r_ui32 i = 0; i < flagNum; ++i ) {
		if ( flagTop[i] != 0xffffffff ) {
			for ( r_si32 j = 0; j < 32; ++j ) {
				if ( ( flagTop[i] & ( 1 << j ) ) == 0 ) {
#ifdef RDEBUG_BUILD
					++numAlloc;
					sumAlloc += blockSize;
#endif // RDEBUG_BUILD
					flagTop[i] |= ( 1 << j );
					return reinterpret_cast<void*> ( reinterpret_cast<size_t> ( blockTop ) + ( ( i * 32 ) + j ) * blockSize );
				}
			}
		}
	}
	return NULL;
}

void* cRAllocBlock :: realloc ( void *pt, r_size size )
{
	if ( size == 0 ) {
		free ( pt );
		return NULL;
	}
	if ( size < blockSize ) {
		if ( pt == NULL ) {
			return malloc ( size );
		}
		return pt;
	}
	RDEBUG_ASSERTMSG ( false, _RST ( "cRAllocBlock::realloc size is large\n" ) );
	return NULL;
}

void cRAllocBlock :: free ( void *pointer )
{
	// アドレスをブロック位置に変換
	r_si32 pos = static_cast<r_si32> ( ( reinterpret_cast<size_t> ( pointer ) - reinterpret_cast<size_t> ( blockTop ) ) / blockSize );
	// アサート
	RDEBUG_ASSERT ( pos >= 0 );
	RDEBUG_ASSERT ( ( flagTop[ ( pos / 32 ) ] & ( 1 << ( pos % 32 ) ) ) != 0 );

	// ブロックを還元
	flagTop[ ( pos / 32 ) ] &= ~ ( 1 << ( pos % 32 ) );
#ifdef RDEBUG_BUILD
	--numAlloc;
	sumAlloc -= blockSize;
#endif // RDEBUG_BUILD
}


